#include "T_Script.h"
#include "cocos2d.h"
#include "Tools/FileBin.h"

namespace LTGame
{
	ScriptData::ScriptData(int levelId)
		: m_curID(levelId)
	{
		scr_scriptCount = 0;
		scr_scriptConditions = NULL;
		scr_scriptConducts = NULL;
		ptlen_sc = NULL;
		ptlen_Conditions = NULL;
		ptlen_Conducts = NULL;
		scr_objScriptRun = NULL;

		_Script_Load_SD();
		_Script_Load_ST();
		runScriptPauseTimer = 0;
	}


	ScriptData::~ScriptData(void)
	{
		for ( int i = 0; i < scr_scriptCount; ++i ){
			for ( int j = 0; j < ptlen_sc[i]; ++j ){
				CC_SAFE_DELETE_ARRAY(scr_scriptConditions[i][j]);
				CC_SAFE_DELETE_ARRAY(scr_scriptConducts[i][j]);
			}
		}
		for ( int i = 0; i < scr_scriptCount; ++i ){
			CC_SAFE_DELETE_ARRAY(scr_scriptConditions[i]);
			CC_SAFE_DELETE_ARRAY(scr_scriptConducts[i]);
		}
		CC_SAFE_DELETE_ARRAY(ptlen_sc);
		CC_SAFE_DELETE_ARRAY(scr_scriptConditions);
		CC_SAFE_DELETE_ARRAY(scr_scriptConducts);

		for ( int i = 0; i < scr_scriptCount; ++i ){
			CC_SAFE_DELETE_ARRAY(ptlen_Conditions[i]);
			CC_SAFE_DELETE_ARRAY(ptlen_Conducts[i]);
		}
		CC_SAFE_DELETE_ARRAY(ptlen_Conditions);
		CC_SAFE_DELETE_ARRAY(ptlen_Conducts);

		scr_objScriptRun = NULL;
	}

	void ScriptData::_Script_Load_SD()
	{
		char tem[32] = {0};
		/*    %d.sd -> %d.gametxt2     */
		sprintf(tem,"GameRes/%d.gametxt2", m_curID);
		FileBin* dis = new FileBin(tem);

		scr_scriptCount = dis->readShort();
		scr_scriptConditions = new byte**[scr_scriptCount];
		scr_scriptConducts = new byte**[scr_scriptCount];
		ptlen_sc = new short[scr_scriptCount];
		ptlen_Conditions = new short*[scr_scriptCount];
		ptlen_Conducts = new short*[scr_scriptCount];

		for ( int i = 0; i < scr_scriptCount; i++ )
		{
			short subcount = dis->readShort();
			ptlen_sc[i] = subcount;
	
			scr_scriptConditions[i] = new byte*[subcount];
			ptlen_Conditions[i] = new short[subcount];
			for ( int j = 0; j < subcount; j++)
			{
				int len = dis->readInt();
				ptlen_Conditions[i][j] = len+2;
				scr_scriptConditions[i][j] = new byte[len+2];
				if( len + 2 != dis->readData( (char*)(scr_scriptConditions[i][j]), len+2) ){
					assert(false);
				}
			}

			scr_scriptConducts[i] = new byte*[subcount];
			ptlen_Conducts[i] = new short[subcount];
			for ( int j = 0; j < subcount; j++)
			{
				int len = dis->readInt();
				ptlen_Conducts[i][j] = len + 1;
				scr_scriptConducts[i][j] = new byte[len+1];
				if( len + 1 != dis->readData( (char*)(scr_scriptConducts[i][j]), len+1) ){
					assert(false);
				}
			}
		}

		CC_SAFE_DELETE(dis);
	}

	void ScriptData::_Script_Load_ST()
	{
		char tem[32] = {0};
		/*    %d.st -> %d.gametxt1     */
		sprintf(tem,"GameRes/%d.gametxt1", m_curID);
		FileBin* dis = new FileBin(tem);

		scr_DialogTextCount = dis->readShort();
		scr_DialogText = new char*[scr_DialogTextCount];
		scr_DialogSignInfo = new short*[scr_DialogTextCount];
		for ( int i = 0; i < scr_DialogTextCount; i++ )
		{
			short sc = dis->readShort();
			scr_DialogSignInfo[i] = new short[sc];
			for ( int j = 0; j < sc; j++){
				scr_DialogSignInfo[i][j] = dis->readShort();
			}
			scr_DialogText[i] = dis->readUTF();
		}
		CC_SAFE_DELETE(dis);
	}
	char *ScriptData::GetPageStr_ScriptDLG(char*str, short start, short end)
	{
		short totalLen = str_CharNum_C(str);
		char* temp = NULL;

		if ( start > totalLen ){
			return NULL;
		}

		if ( end > totalLen ){
			ARRAY_NEW(temp, char, 2*(totalLen - start)+1 );
			memcpy(temp, str + 2*start, 2*(totalLen - start) );
		}
		else{
			ARRAY_NEW(temp, char, 2*(end - start) + 1);
			memcpy(temp, str + 2*start, 2*(end - start));
		}

		return temp;
	}

	int ScriptData::str_CharNum_C(char* str)
	{
		byte* p;
		int len;
		int num = 0;

		assert(str);
		len = strlen(str);
		p = (byte*)str;

		while(*p) {
			assert(p - (byte*)str < len);
			if(*p < _STR_GB_CHAR_START) {	// ansi byte
				p++;
			}
			else {
				p += 2;
			}
			num++;
		}
		return num;
	}
	bool ScriptData::str_IsGBChar(char* p_char) {
		assert(p_char);
		if( (*(byte*)p_char) >= _STR_GB_CHAR_START) {
			return true;
		}
		return false;
	}
}